<template>
    <div>
        <avue-form ref="form" v-model="obj" :option="option"
                   @reset-change="emptytChange"
                   @submit="submit">

        </avue-form>
    </div>
</template>

<script>

    import {baseUploadUrl} from "@/config/env";
    export default {
        data() {
            return {
                obj: {},
                isIdFind: true,
                initSuccess: false,
            }
        },
        props: {
            closeDialog: [],
            uri: {},
            rowData: {},
        },
        computed: {
            option() {
                return {
                    submitBtn: true,
                    emptyBtn: true,
                    submitText: '提交',
                    emptyText: "关闭",
                    column: [
                        {
                            label: '账号',
                            prop: 'username',
                            maxlength: 32,
                            showWordLimit: true,
                            span: 20,
                            //display : false,
                            disabled: true,
                            rules: [{
                                required: false,
                                message: "请输入 账号",
                                trigger: "blur"
                            }]
                        },
                        {
                            label: '邮箱地址',
                            prop: 'email',
                            maxlength: 32,
                            showWordLimit: true,
                            span: 20,
                            //display : false,
                            disabled: true,
                            rules: [{
                                required: false,
                                message: "请输入 邮箱地址",
                                trigger: "blur"
                            }]
                        },
                        {
                            label: '手机号',
                            prop: 'phone',
                            maxlength: 32,
                            showWordLimit: true,
                            span: 20,
                            //display : false,
                            disabled: true,
                            rules: [{
                                required: false,
                                message: "请输入 手机号",
                                trigger: "blur"
                            }]

                        },

                        {
                            label: '头像',
                            prop: 'headPic',
                            span: 24,
                            rules: [{
                                required: false,
                                message: "请上传 头像 ",
                                trigger: "blur"
                            }],
                            dataType: 'string',  // 字符串模式
                            type: 'upload',
                            listType: 'picture-img',                // 图片格式, 单图-[picture-img]  多图-[picture-card]  缩略图-[picture] 普通文件空
                            action: baseUploadUrl + 'image/gc/',    // 上传地址 + 文件保存上传地址(详见接口描叙)
                            multiple: false,       // 文件多选
                            drag: true,            // 拖拽排序
                            limit: 1,              // 上传数量 1 个
                            //fileSize: 500,         // 上传大小 500 kb内
                            loadText: '上传中...',  // 上传中文字提示
                            tip: '只能上传jpg/png/gif文件',
                            propsHttp: {
                                res: 'data'
                            },
                            uploadBefore: (file, done) => {
                                // 文件上传前处理
                                done(file)
                            },
                            uploadAfter: (res, done) => {
                                this.$message.success('上传成功');
                                done()
                            },
                            uploadError(error, column) {
                                // 上传失败
                                this.$message.error(error);
                            },
                            uploadExceed(limit, files, fileList, column){
                                // 文件数量验证
                                this.$message.warning(`当前限制文件数量为 ${limit}, 当前共 ${files.length + fileList.length} `);
                            },
                        },
                        {
                            label: '姓名',
                            prop: 'fullName',
                            maxlength: 32,
                            showWordLimit: true,
                            span: 20,
                            rules: [{
                                required: false,
                                message: "请输入 姓名",
                                trigger: "blur"
                            }]
                        },
                        {
                            label: '昵称',
                            prop: 'nickname',
                            maxlength: 32,
                            showWordLimit: true,
                            span: 20,
                            rules: [{
                                required: false,
                                message: "请输入 昵称",
                                trigger: "blur"
                            }]
                        },
                        {
                            label: '性别 ',
                            prop: 'gender',
                            type: 'radio',
                            dicData: this.dict.get('GENDER'),
                            span: 20,
                            rules: [{
                                required: false,
                                message: "请选择 性别 ",
                                trigger: "blur"
                            }]
                        },
                        {
                            label: '地址',
                            prop: 'address',
                            maxlength: 128,
                            showWordLimit: true,
                            span: 20,
                            rules: [{
                                required: false,
                                message: "请输入 地址",
                                trigger: "blur"
                            }]
                        },
                        {
                            label: '年龄',
                            prop: 'age',
                            type: 'number',
                            precision: 0,  //保留小数位,
                            minRows: 0,
                            maxRows: 99999999,
                            row: true,
                            span: 20,
                            rules: [{
                                required: false,
                                message: "请输入 年龄",
                                trigger: "blur"
                            }]
                        },
                    ]
                }
            }
        },
        created() {
            if (this.isIdFind) {
                this.findId(this.rowData);
            } else {
                this.obj = this.rowData;
                this.initSuccess = true;
            }
        },
        methods: {
            emptytChange() {
                this.closeDialog(false);
            },
            submit(form, done) {
                this.crud.put(this.uri.info + "/" + this.obj.id, this.obj).then((res) => {
                    console.debug(res);
                    if (res.data.code == 200) {
                        this.closeDialog(true);
                    }
                    done(form);
                }).catch(err => {
                    console.error(err);
                    done(form);
                })
            },
            findId(rowData) {
                if (rowData != null && rowData.id != null) {
                    this.crud.get(this.uri.info + "/" + rowData.id).then((res) => {
                         this.obj = res.data.data;
                         this.initSuccess = true;
                    })
                }
             }
        }
    }
</script>
